package com.atwisdom.star.core.bsl.planner.Impl;

import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.core.bsl.planner.MaterialBslService;
import com.atwisdom.star.core.bsl.planner.entity.Material;
import com.atwisdom.star.core.dsl.modelClass.consume.Impl.ConsumeInstanceDslServiceImpl;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Data
@NoArgsConstructor
public class MaterialBslServiceImpl implements MaterialBslService {

    private ConsumeInstanceDslServiceImpl consumeInstanceDslService;

    @Autowired
    public void setConsumeInstanceDslService(ConsumeInstanceDslServiceImpl consumeInstanceDslService) {
        this.consumeInstanceDslService = consumeInstanceDslService;
    }

    //物料信息
    private List<Material> materials = new ArrayList<>();
    //物料属性Map
    private Map<String,String> materialAttrMap = new HashMap<>();

    /**
     * 获取物料及安全库存
     *
     * @return
     */
    public void initMaterial() {
        initMaterialByCondition(null);
    }

    public void initMaterialByCondition(String condition) {
        this.materials = new ArrayList<>();
        this.materialAttrMap = new HashMap<>();
        List<JSONObject> materialList = this.consumeInstanceDslService.query(condition, "material", null);
        for (JSONObject jsonObject : materialList) {
            Long id = jsonObject.getLong("id");
            JSONObject values = jsonObject.getJSONObject("values");
            String pCode = values.getString("pcode");
            String bCode = values.getString("bcode");
            String carModel = values.getString("carModel");
            String spec = values.getString("spec");
            String model = values.getString("model");
            String unit = values.getString("unit");
            String subUnit = values.getString("subUnit");
            Double referToPrice = values.getDouble("referToPrice");
            Double conversionRate = values.getDouble("conversionRate");

            String name = values.getString("name");
            String mType = values.getString("mtype");
            String attr = values.getString("attr");
            //3、安全库存
            Integer safeStock = values.getInteger("safeStock");
            if (safeStock == null) {
                safeStock = 0;
            }
            //最大库存
            Integer maxStock = values.getInteger("maxStock");
            if (maxStock == null || maxStock.intValue() == 0) {
                maxStock = 999999999;
            }
            //生产最小批量
            Integer minBatch = values.getInteger("minBatch"); //生产最小批量
            if(minBatch == null){
                minBatch = 1;
            }
            //生产领用倍量
            Integer useMultiple = values.getInteger("useMultiple");
            if (useMultiple == null) {
                useMultiple = 1;
            }
            //采购最小包装
            Integer minPackage = values.getInteger("minPackage"); //采购最小包装量
            if (minPackage == null) {
                minPackage = 1;
            }
            //收容数
            Integer acceptQuantity = values.getInteger("acceptQuantity"); //采购最小包装量
            if (acceptQuantity == null) {
                acceptQuantity = 1;
            }

            String routing = values.getString("routing");
            Integer purchasingPeriod = values.getInteger("purchasingPeriod");
            Integer outsourcingPeriod = values.getInteger("outsourcingPeriod");
            Integer producePeriod = values.getInteger("producePeriod");

            Double produceLeadTime = values.getDouble("produceLeadTime");
            if(produceLeadTime==null){
                produceLeadTime = 0.0;
            }

            Integer saleLeadTime = values.getInteger("saleLeadTime");
            Integer purchasingLeadTime = values.getInteger("purchasingLeadTime");
            Integer outsourcingLeadTime = values.getInteger("outsourcingLeadTime");
            String dispatchFlag = values.getString("dispatchFlag");
            Long defaultWarehouseId = values.getLong("defaultWarehouseId");
            String defaultWarehouseName = values.getString("defaultWarehouseName");
            String defaultContainer = values.getString("defaultContainer");
            Integer supplierId = values.getInteger("supplier_id");
            String supplierCode = values.getString("supplier_code");
            String supplierName = values.getString("supplier_name");

            Integer outsourcingSupplierId = values.getInteger("outsourcingSupplier_id");
            String outsourcingSupplierCode = values.getString("outsourcingSupplier_code");
            String outsourcingSupplierName = values.getString("outsourcingSupplier_name");

            Material material = new Material();
            material.setId(id);
            material.setPcode(pCode);
            material.setBcode(bCode);
            material.setCarModel(carModel);
            material.setSpec(spec);
            material.setModel(model);
            material.setUnit(unit);
            material.setSubUnit(subUnit);
            material.setReferToPrice(referToPrice);
            material.setConversionRate(conversionRate);

            material.setAttr(attr);
            material.setName(name);
            material.setMType(mType);
            material.setSafeStock(safeStock);
            material.setMaxStock(maxStock);
            material.setMinBatch(minBatch);
            material.setMinPackage(minPackage);
            material.setUseMultiple(useMultiple);
            material.setAcceptQuantity(acceptQuantity);
            material.setDefaultContainer(defaultContainer);
            material.setRouting(routing);
            material.setPurchasingPeriod(purchasingPeriod);
            material.setOutsourcingPeriod(outsourcingPeriod);
            material.setProducePeriod(producePeriod);

            material.setProduceLeadTime(produceLeadTime);
            material.setSaleLeadTime(saleLeadTime);
            material.setPurchasingLeadTime(purchasingLeadTime);
            material.setOutsourcingLeadTime(outsourcingLeadTime);
            material.setDispatchFlag(dispatchFlag);
            material.setWarehouseId(defaultWarehouseId);
            material.setWarehouseName(defaultWarehouseName);
            material.setSupplierId(supplierId);
            material.setSupplierCode(supplierCode);
            material.setSupplierName(supplierName);

            material.setOutsourcingSupplierId(outsourcingSupplierId);
            material.setOutsourcingSupplierCode(outsourcingSupplierCode);
            material.setOutsourcingSupplierName(outsourcingSupplierName);
            this.materials.add(material);
            this.materialAttrMap.put(bCode,attr);
        }
    }
}
